home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9987 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.5 KB  |  190 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: HELP!
  5. Date: Thu, 14 Mar 96 20:02:17 GMT
  6. Organization: none
  7. Message-ID: <826833737snz@genesis.demon.co.uk>
  8. References: <4htsjm$v5o@lantana.singnet.com.sg> <3142BF08.1F5C@hsc.unt.edu>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <3142BF08.1F5C@hsc.unt.edu>
  15.            sfogoros@hsc.unt.edu "Steve Fogoros" writes:
  16.  
  17. >Teddy Bear wrote:
  18. >> 
  19. >> Can anyone please help me my giving me a sample programme?
  20. >> I need to write a program that will enter and record an individual's
  21. >> details
  22. >> search (by name) and display an individual's details
  23. >> Delete an individual's details
  24. >> Save and restore the entire list to/from a disk file
  25. >> 
  26. >> Emergency!
  27. >> Thanks
  28. >> my e-mail add is at s7700038@singnet.com.sg
  29. >
  30. >Here is a most basic sample program:
  31. >
  32. >#include "stdio.h"
  33. >#include "stdlib.h"
  34. >#include "string.h"
  35.  
  36. These should be:
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41.  
  42. >void enter(void);
  43. >void show(void);
  44. >void delete(void);
  45. >
  46. >FILE *fi, *fo;
  47. >
  48. >void main(void)
  49.  
  50. main returns int in C. 
  51.  
  52.  int main(void)
  53.  
  54. >{
  55. >   printf("1. enter a record\n");
  56. >   printf("2. show a record\n");
  57. >   printf("3. delete a record\n");
  58. >   printf("4. exit\n");
  59. >   printf("Press 1, 2, 3, or 4 then enter\n");
  60. >
  61. >   switch((char)getc(stdin))
  62.  
  63. getc(stdin) is more commonly called getchar()
  64.  
  65. The (char) cast has no effect other than possibly to introduce implementation
  66. defined behaviour - you're better off without it. It is worth noting as
  67. a distantly related issue that character constants such as '1' have type int
  68. in C.
  69.  
  70. >   {
  71. >   case '1': getc(stdin); enter(); break;
  72. >   case '2': getc(stdin); show(); break;
  73. >   case '3': getc(stdin); delete(); break;
  74. >   case '4': exit(0); break;
  75. >   default: printf("bad selection. program terminated\n"); exit(0); break;
  76. >   }
  77.  
  78.     return 0;
  79.  
  80. >}
  81. >
  82. >void enter(void)
  83. >{
  84. >   char buff[80];
  85. >
  86. >   printf("Enter data followed by return key\n");
  87. >   fgets(buff,79,stdin);
  88.  
  89. This is OK but you could also have written:
  90.  
  91.     fgets(buff,80,stdin);
  92.  
  93. or better still
  94.  
  95.     fgets(buff,sizeof buff,stdin);
  96.  
  97. the 2nd argument specifies the maximum number of characters that fgets()
  98. will write to the buffer including the terminating '\0'
  99.  
  100. >
  101. >   fi = fopen("dfile","a");
  102. >   if(fi == 0)
  103.  
  104. Tha's fine but you might consider demonstrating the pointer comparison
  105. explicitly with:
  106.  
  107.     if(fi == NULL)
  108.  
  109. >   {
  110. >      printf("unable to open dfile. program terminated\n");
  111. >      exit(0);
  112.  
  113. Since this is a failure state you might also consider:
  114.  
  115.        exit(EXIT_FAILURE);
  116.  
  117. EXIT_SUCCESS and EXIT_FAILURE are defined in stdlib.h. 0 return is an
  118. alternative to EXIT_SUCCESS for indicating success.
  119.  
  120. >   }
  121. >
  122. >   fprintf(fi,"%s",buff);
  123. >
  124. >   fclose(fi);
  125. >}
  126. >
  127. >void show(void)
  128. >{
  129. >   char srch[80], buff[80];
  130. >
  131. >   printf("Enter search string and enter\n");
  132. >   fgets(srch,79,stdin);
  133.  
  134. Same as before.
  135.  
  136. >   srch[strlen(srch)-1] = 0;
  137.  
  138. This is dangerous - it is possible for fgets() to put a 0 length string in
  139. srch. It is also possible that the last character before '\0' is not '\n',
  140. not to mention the possibility of fgets() writing no characters to srch
  141. because of an end-of-file or error condition. Some of these thing can perhaps
  142. be ignored for keyboard input (but IMHO it is not a good idea) there
  143. is a simple trick that has recently been making the rounds of comp.lang.c
  144. which copes with the first two problems:
  145.  
  146.      strtok(srch, "\n");
  147.  
  148. >   printf("\n");
  149.  
  150. There is also putchar('\n') which is the tool designed for this particular
  151. job. I know others prefer the uniformity of using printf everywhere.
  152.  
  153. >   fi = fopen("dfile","r");
  154. >   if(fi == 0)
  155. >   {
  156. >      printf("unable to open dfile. program terminated\n");
  157. >      exit(0);
  158. >   }
  159. >
  160. >   while(!feof(fi))
  161. >   {
  162. >      fgets(buff,79,fi);
  163. >      if(feof(fi)) break;
  164. >      if(strstr(buff,srch)) printf("%s",buff);
  165. >   }
  166.  
  167. It is quite rare that you need to use feof() needs to be used.
  168. Most standard library function have return values and they are there to
  169. be used. So:
  170.  
  171.     while(fgets(buff,sizeof buff,fi) != NULL)
  172.     {
  173.         if(strstr(buff,srch)) printf("%s",buff);
  174.     }
  175.  
  176. The loop test exits the loop on both an end-of-file and error condition
  177. If you wanted to find out wich occurred you could use ferror() or feof()
  178. here.
  179.  
  180. >   fclose(fi);
  181. >}
  182.  
  183. ...
  184.  
  185. -- 
  186. -----------------------------------------
  187. Lawrence Kirby | fred@genesis.demon.co.uk
  188. Wilts, England | 70734.126@compuserve.com
  189. -----------------------------------------
  190.